react 4.0 路由怎么跳转-环球速递

来源:php中文网 | 2023-01-06 10:10:15 |

本教程操作环境:Windows10系统、React-Router v4.0版、Dell G3电脑。

react 4.0 路由怎么跳转?


(资料图片仅供参考)

React-Router v4.0 hashRouter使用js跳转

React-Router v4.0上已经不推荐使用hashRouter,主推browserRouter,但是因为使用browserRouter需要服务端配合可能造成不便,有时还是需要用到hashRouter。

下面是v4.0的React-Router中hashRouter以js方式跳转的实现步骤。

v4.0剥离了history,所以要操作history,需要安装支持包:

npm install history --save

在要跳转的地方对应的js文件中,引入createHashHistory并执行代码,以跳转到"/share"举例:

import { createHashHistory } from "history"createHashHistory().push("/share")

已经ok了。

在使用上述方法跳转之前,需要确认已经定义Router,可参考下述代码:

import { HashRouter as Router, Route, Switch } from "react-router-dom"...<Router>  <App>    <Switch>      <Route path="/index" component={显示的组件1}>      <Route path="/share" component={显示的组件2}>      ...    </Switch>  </App></Router>

推荐学习:《react视频教程》

以上就是react 4.0 路由怎么跳转的详细内容,更多请关注php中文网其它相关文章!

关键词: react-router